*** Modified by SBo:
*** Was: PNG_LIBPNG_VER_MINOR < 5
*** Now: PNG_LIBPNG_VER_MINOR < 4
From 70c7f1967f43a94f9f0d6808d6ab5700a120d2fc Mon Sep 17 00:00:00 2001
From: Chris Liddell <chris.liddell@artifex.com>
Date: Fri, 17 Apr 2015 12:19:26 +0100
Subject: [PATCH] Bug 695890: Update jbig2dec for modern libpng

libpng 1.5.x and newer hide the internals, and only allow values to be set and
retrieved via define API calls.

Also tidy up a couple of (benign) compiler warnings.

No cluster diffs.
---
 jbig2_arith.c      |  9 +++++++--
 jbig2_image_pbm.c  |  2 +-
 jbig2_image_png.c  | 23 ++++++++++++++++++-----
 jbig2_refinement.c |  4 ++++
 jbig2_text.c       |  2 ++
 5 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/jbig2_arith.c b/jbig2_arith.c
index 5e6cf4c..b65022e 100644
--- a/jbig2_arith.c
+++ b/jbig2_arith.c
@@ -422,9 +422,14 @@ main (int argc, char **argv)
 
   for (i = 0; i < 256; i++)
     {
-      bool D;
+#ifdef JBIG2_DEBUG_ARITH
+      bool D =
+#else
+      (void)
+#endif
+
+      jbig2_arith_decode (as, &cx);
 
-      D = jbig2_arith_decode (as, &cx);
 #ifdef JBIG2_DEBUG_ARITH
       fprintf(stderr, "%3d: D = %d, ", i, D);
       jbig2_arith_trace (as, cx);
diff --git a/jbig2_image_pbm.c b/jbig2_image_pbm.c
index 0ad7e32..49bf756 100644
--- a/jbig2_image_pbm.c
+++ b/jbig2_image_pbm.c
@@ -145,7 +145,7 @@ Jbig2Image *jbig2_image_read_pbm(Jbig2Ctx *ctx, FILE *in)
     }
     /* the pbm data is byte-aligned, so we can
        do a simple block read */
-    fread(image->data, 1, image->height*image->stride, in);
+    (void)fread(image->data, 1, image->height*image->stride, in);
     if (feof(in)) {
         fprintf(stderr, "unexpected end of pbm file.\n");
         jbig2_image_release(ctx, image);
diff --git a/jbig2_image_png.c b/jbig2_image_png.c
index 6b2d9b9..1dd4e45 100644
--- a/jbig2_image_png.c
+++ b/jbig2_image_png.c
@@ -26,7 +26,11 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <png.h>
+
+#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 4
 #include <pngstruct.h>
+#endif
+
 #define CVT_PTR(ptr) (ptr)
 
 #include "jbig2.h"
@@ -39,8 +43,13 @@ static void
 jbig2_png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
 {
     png_size_t check;
+#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 4
+    png_FILE_p f = (png_FILE_p)png_ptr->io_ptr;
+#else
+    png_FILE_p f = (png_FILE_p)png_get_io_ptr(png_ptr);
+#endif
 
-    check = fwrite(data, 1, length, (png_FILE_p)png_ptr->io_ptr);
+    check = fwrite(data, 1, length, f);
     if (check != length) {
       png_error(png_ptr, "Write Error");
     }
@@ -49,10 +58,14 @@ jbig2_png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
 static void
 jbig2_png_flush(png_structp png_ptr)
 {
-    png_FILE_p io_ptr;
-    io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
-    if (io_ptr != NULL)
-        fflush(io_ptr);
+#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 4
+    png_FILE_p f = (png_FILE_p)png_ptr->io_ptr;
+#else
+    png_FILE_p f = (png_FILE_p)png_get_io_ptr(png_ptr);
+#endif
+
+    if (f != NULL)
+        fflush(f);
 }
 
 int jbig2_image_write_png_file(Jbig2Image *image, char *filename)
diff --git a/jbig2_refinement.c b/jbig2_refinement.c
index 31b0bfe..b456173 100644
--- a/jbig2_refinement.c
+++ b/jbig2_refinement.c
@@ -38,6 +38,7 @@
 #include "jbig2_generic.h"
 #include "jbig2_image.h"
 
+#if 0 /* currently not used */
 static int
 jbig2_decode_refinement_template0(Jbig2Ctx *ctx,
                               Jbig2Segment *segment,
@@ -49,6 +50,7 @@ jbig2_decode_refinement_template0(Jbig2Ctx *ctx,
   return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
     "refinement region template 0 NYI");
 }
+#endif
 
 static int
 jbig2_decode_refinement_template0_unopt(Jbig2Ctx *ctx,
@@ -159,6 +161,7 @@ jbig2_decode_refinement_template1_unopt(Jbig2Ctx *ctx,
   return 0;
 }
 
+#if 0 /* currently not used */
 static int
 jbig2_decode_refinement_template1(Jbig2Ctx *ctx,
                               Jbig2Segment *segment,
@@ -241,6 +244,7 @@ jbig2_decode_refinement_template1(Jbig2Ctx *ctx,
   return 0;
 
 }
+#endif
 
 
 typedef uint32_t (*ContextBuilder)(const Jbig2RefinementRegionParams *,
diff --git a/jbig2_text.c b/jbig2_text.c
index 5c9df26..3fdd3a5 100644
--- a/jbig2_text.c
+++ b/jbig2_text.c
@@ -412,6 +412,7 @@ cleanup1:
 		case JBIG2_CORNER_TOPLEFT: x = S; y = T; break;
 		case JBIG2_CORNER_TOPRIGHT: x = S - IB->width + 1; y = T; break;
 		case JBIG2_CORNER_BOTTOMLEFT: x = S; y = T - IB->height + 1; break;
+		default:
 		case JBIG2_CORNER_BOTTOMRIGHT: x = S - IB->width + 1; y = T - IB->height + 1; break;
 		}
 	    } else { /* TRANSPOSED */
@@ -419,6 +420,7 @@ cleanup1:
 		case JBIG2_CORNER_TOPLEFT: x = T; y = S; break;
 		case JBIG2_CORNER_TOPRIGHT: x = T - IB->width + 1; y = S; break;
 		case JBIG2_CORNER_BOTTOMLEFT: x = T; y = S - IB->height + 1; break;
+		default:
 		case JBIG2_CORNER_BOTTOMRIGHT: x = T - IB->width + 1; y = S - IB->height + 1; break;
 		}
 	    }
-- 
2.5.1

